﻿#include "cam.h"
#include "ui_cam.h"

#include <QCamera>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QVBoxLayout>
//#include <QWebEngineSettings>
#include "mywebenginepage.h"

Cam::Cam(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cam)
{
    ui->setupUi(this);
    QCamera *camera;
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();

//    foreach (const QCameraInfo &cameraInfo, cameras) {
//        //qDebug() << "camera" <<  cameraInfo.deviceName();
//        if (cameraInfo.deviceName() == "@device:pnp:\\\\?\\usb#vid_05c8&pid_0b06&mi_00#7&21b6f2bc&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global")
//            camera = new QCamera(cameraInfo);
//    }

//         //= new QCamera(QCamera::DefaultDevice, this);
        QCameraViewfinder *viewfinder;

//        camera->setViewfinder(viewfinder);

        // 创建摄像头对象
        camera = new QCamera(QCamera::UnspecifiedPosition, this);

        // 创建视图查找器
        viewfinder = new QCameraViewfinder(this);

        // 设置视图查找器为摄像头的输出目标
        camera->setViewfinder(viewfinder);

        // 创建布局并将视图查找器添加到布局中
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(viewfinder);

        // 启动摄像头
        camera->start();
//        layout->addWidget(viewfinder);

//        window.setCentralWidget(centralWidget);
//        window.setWindowTitle("Qt Camera Example");
//        window.resize(800, 600);

//    //    // 创建 QWebEngineView
//    QWebEngineView *view = new QWebEngineView(ui->widgetWeb);
//    view->setGeometry(222,30,650,500);
//    QWebEngineSettings *settings = view->settings();
//    settings->setAttribute(QWebEngineSettings::PluginsEnabled, true);
//    settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
//    view->setPage(new MyWebEnginePage);
//    view->load(QUrl("https://v.ifeng.com/c/8WUeT1Pv0go"));

//    connect(view, &QWebEngineView::loadFinished, [view](bool) {
//        QString script = "document.body.style.backgroundColor = 'rgb(44, 46, 67)';";

//        view->page()->runJavaScript(script);
//    });
}

Cam::~Cam()
{
    delete ui;
}

